articleComment();

function articleComment() {
    var articleId = "31";
    //填充文章评论
    $.ajax({
        type: 'get',
        url: '/getCommentByArticleID',
        dataType: 'json',
        async: false,
        data: {
            articleID: articleId
        },
        success: function (data) {
            if (data['status'] == 200) {
                console.log("已获取文章评论");
                addCommentList(data['result']);
            } else {
                //暂无评论展示
            }
        },
        error: function () {
            alert("请求服务器时发生错误");
        }
    });
    //添加文章评论
    $("#comment").click(function () {
        var content = $("#content").val();
        var osName = getOperationSys();
        $.ajax({
            type: 'post',
            url: '/saveComment',
            dataType: 'json',

            data: {
                articleID: articleId,
                content: content,
                osName: osName
            },
            success: function (data) {
                if (data['status'] == 200) {
                    //评论成功
                    alert("评论成功");
                    window.location.reload()
                } else {
                    //评论失败
                }
            },
            error: function () {
                alert("请求服务器失败")
            }
        })
    });

    function addCommentList(data) {
        var list = $(".comment-list");
        list.empty();
        $.each(data, function (index, object) {

            if (object['replyID'] == null) {

                var comment = "<div class='comment-info'>" +
                    "<header><img src='" + object['img'] + "'></header>" +
                    "<div class='comment-right'>" +
                    "<h3>" + object['replyName'] + "</h3>" +
                    "<div class='comment-content-header'><span><i class='glyphicon glyphicon-time'></i>" + object['time'] +
                    "</span></div>" +
                    "<p class='content'style='margin-left: -50px;  font-size: x-large;'>" + object['content'] + "</p>" +
                    "<div class='comment-content-footer'>" +
                    "<div class='row'><div class='col-md-10'><span><i class='glyphicon glyphicon-globe'></i>" + object['osName'] + "</span></div><div class='col-md-2'><span class='reply-btn'>回复</span></div></div>" +
                    "</div>" +
                    "<div id='commentID-" + object['id'] + "' class='reply-list'></div>" +
                    "</div>" +
                    "</div>";
                list.append(comment);
                //填充评论回复

                console.log(object['id']);
                $.ajax({
                    type: 'get',
                    url: '/getReplyByCommentID',
                    dataType: 'json',
                    async: false,//改为同步执行
                    data: {
                        id: object['id']
                    },
                    success: function (data) {

                        if (data['status'] == 200) {

                            $.each(data['result'], function (index, o) {

                                if (object['id'] == o['beReplyCommentID']) {

                                    var replyEl = "<div class='reply'><div><a href='javascript:void(0)' class='replyname'>" + o['replyName'] + "</a>:<a href='javascript:void(0)'>@" + o['beReplyName'] + "</a><span>" + o['content'] + "</span></div>"
                                        + "<p><span>" + o['time'] + "</span> <span class='reply-list-btn'>回复</span></p></div>";
                                    var commentId = "#commentID-" + object['id'];
                                    list.find(commentId).append(replyEl);

                                }
                            })
                        }

                    }
                });

            } else {
                console.log("不为空")
            }

        });
        $(".reply-btn").click(function () {
            if ($(this).parent().parent().find(".replybox").length > 0) {
                $(".replybox").remove();
            } else {
                $(".replybox").remove();
                replyClick($(this));
            }
        })
        $(".reply-list-btn").click(function () {
            if ($(this).parent().parent().find(".replybox").length > 0) {
                $(".replybox").remove();
            } else {
                $(".replybox").remove();
                replyClick($(this));
            }
        })


    }

    function replyClick(el) {
        el.parent().parent().append("<div class='replybox'><textarea cols='80' rows='50' placeholder='输入要回复的内容......' class='mytextarea' ></textarea><span class='send'>发送</span></div>").find(".send").click(function () {
            var content = $(this).prev().val();
            if (content != "") {
                var parentEl = $(this).parent().parent().parent().parent();
                var obj = new Object();
                if (el.parent().parent().hasClass("reply")) {
                    obj.beReplyName = el.parent().parent().find("a:first").text();
                    console.log(obj.beReplyName);
                } else {
                    obj.beReplyName = parentEl.find("h3").text();
                    console.log(obj.beReplyName);
                }
                var id = el.parent().parent().parent().attr('id');

                $.ajax({
                    type: 'post',
                    url: '/saveReplyComment',
                    dataType: 'json',
                    data: {
                        articleID: articleId,
                        beReplyName: obj.beReplyName,
                        content: content,
                        replyID: id
                    },
                    success: function (data) {
                        if (data['status'] == 200) {
                            alert("评论成功成功");
                            window.location.reload()
                            //存储成功
                        }
                    }
                });

                $(".replybox").remove();
                parentEl.find(".reply-list").find(".reply-list-btn:last").click(function () {
                    alert("不能回复自己");
                });
            } else {
                alert("输入内容不能为空");
            }
        });
    }


//获得系统名称
    function getOperationSys() {
        var OS = '';
        var OSArray = {};
        var UserAgent = navigator.userAgent.toLowerCase();
        OSArray.Windows = (navigator.platform == 'Win32') || (navigator.platform == 'Windows');
        OSArray.Mac = (navigator.platform == 'Mac68K') || (navigator.platform == 'MacPPC')
            || (navigator.platform == 'Macintosh') || (navigator.platform == 'MacIntel');
        OSArray.iphone = UserAgent.indexOf('iPhone') > -1;
        OSArray.ipod = UserAgent.indexOf('iPod') > -1;
        OSArray.ipad = UserAgent.indexOf('iPad') > -1;
        OSArray.Android = UserAgent.indexOf('Android') > -1;
        for (var i in OSArray) {
            if (OSArray[i]) {
                OS = i;
            }
        }
        return OS;
    }
}


